<!DOCTYPE HTML>
<html>
<head>
	<title>商品详情</title>
	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="static/assets/global/plugins/bootstrap/css/bootstrap.css" />
	<link rel="stylesheet" type="text/css" href="static/assets/global/css/components.css" />
	<link rel="stylesheet" type="text/css" href="static/assets/admin/pages/css/login.css" />
	<script type="text/javascript" src="static/assets/global/plugins/jquery-1.11.0.min.js"></script>
</head>
<body class="login">
	<div class="content">
		<h3 class="form-title">创建详情</h3>
		<div class="form-group">
			 <label class="control-label">商品名</label>
			 <div>
			 	<label class="control-label" id="itemname" />
			 </div>
		</div>

		<div class="form-group">
			 <label class="control-label">商品描述</label>
			 <div>
			 	<label class="control-label" id="description" />
			 </div>
		</div>

		<div class="form-group">
			 <label class="control-label">价格</label>
			 <div>
			 	<label class="control-label" id="price" />
			 </div>
		</div>

		<div class="form-group">
			 <label class="control-label">图片</label>
			 <div>
			 	<img  id="imgurl" style="width:200px;height: auto;">
			 </div>
		</div>

		<div class="form-group">
			 <label class="control-label">库存</label>
			 <div>
			 	<label class="control-label" id="stock" />
			 </div>
		</div>
		<div class="form-group">
			 <label class="control-label">销量</label>
			 <div>
			 	<label class="control-label" id="sales" />
			 </div>
		</div>
		<div class="form-actions">
			<button class="btn blue" id="createOrder" type="submit">提交订单</button>
		</div>
	</div>
</body>
<script type="text/javascript">

	var g_itemVo = {}; 

	jQuery(document).ready(function(){

	//根据URL传参获取参数值（get请求）
		function getParam(paramName) {
        	paramValue = "", isFound = !1;
        	if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
            	arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0;
            	while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++
        	}
        	return paramValue == "" && (paramValue = null),paramValue
    	}
		//获取商品详情接口数据
		$.ajax({type:"GET",
				url:"http://localhost:8081/miaosha/item/getItemInfo",
				data:{
					"id":getParam("id")
				},
				xhrFields:{withCredentials: true},
				success:function(data){
					if(data.status == "success"){
						// window.location.href="login.html"
						g_itemVo = data.data;
						console.log(g_itemVo.title);
						reloadDom();
					}else{
						alert("获取商品详情失败："+data.data.errMsg);
					}
				},
				error:function(data){
					alert("获取商品详情失败："+data.responseText);
				}
			});
	});

	//获取数据模型之后填充数据
	function reloadDom(){
		$("#itemname").text(g_itemVo.title);
		$("#description").text(g_itemVo.description);
		$("#price").text(g_itemVo.price);
		$("#imgurl").attr("src",g_itemVo.imgUrl);
		$("#stock").text(g_itemVo.stock);
		$("#sales").text(g_itemVo.sales);
	}

	//创建订单
	$("#createOrder").on("click",function(){
		$.ajax({type:"POST",
				contentType:"application/x-www-form-urlencoded",
				url:"http://localhost:8081/miaosha/order/createOrder",
				data:{
					"itemId" : g_itemVo.id,
					"amount" : 1
				},
				xhrFields:{withCredentials: true},
				success:function(data){
					if(data.status == "success"){
						alert("下单成功");
						window.location.reload();
					}else{
						alert("下单失败："+data.data.errMsg);
						if(data.data.errCode == 200004){
							window.location.href = "login.html";
						}
					}
				},
				error:function(data){
					alert("下单失败："+data.responseText);
				}
			});
	});
</script>
</html>